package com.eduagent.xwqeduagent.api.characterRecognition.HandwrittenTextRecognition;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 手写文字识别结果解析工具类
 */
public class HandwrittenTextRecognitionResult {
    private String code;
    private String desc;
    private String sid;
    private List<String> recognizedTexts;
    private boolean success;

    /**
     * 解析识别结果JSON字符串
     * @param jsonResult 识别结果JSON字符串
     * @return 解析后的结果对象
     */
    public static HandwrittenTextRecognitionResult parseResult(String jsonResult) {
        HandwrittenTextRecognitionResult result = new HandwrittenTextRecognitionResult();
        
        // 检查JSON字符串是否为空
        if (jsonResult == null || jsonResult.trim().isEmpty()) {
            result.success = false;
            result.code = "-1";
            result.desc = "API返回结果为空";
            return result;
        }
        
        try {
            // 尝试解析JSON
            JSONObject jsonObject = JSON.parseObject(jsonResult);
            if (jsonObject == null) {
                result.success = false;
                result.code = "-1";
                result.desc = "JSON解析失败，返回内容不是有效的JSON格式";
                return result;
            }
            
            result.code = jsonObject.getString("code");
            result.desc = jsonObject.getString("desc");
            result.sid = jsonObject.getString("sid");
            result.success = "0".equals(result.code);
            
            if (result.success && jsonObject.containsKey("data")) {
                result.recognizedTexts = new ArrayList<>();
                JSONObject dataObj = jsonObject.getJSONObject("data");
                if (dataObj.containsKey("block")) {
                    JSONArray blockArray = dataObj.getJSONArray("block");
                    for (int i = 0; i < blockArray.size(); i++) {
                        JSONObject blockObj = blockArray.getJSONObject(i);
                        if ("text".equals(blockObj.getString("type")) && blockObj.containsKey("line")) {
                            JSONArray lineArray = blockObj.getJSONArray("line");
                            for (int j = 0; j < lineArray.size(); j++) {
                                JSONObject lineObj = lineArray.getJSONObject(j);
                                if (lineObj.containsKey("word")) {
                                    JSONArray wordArray = lineObj.getJSONArray("word");
                                    StringBuilder lineText = new StringBuilder();
                                    for (int k = 0; k < wordArray.size(); k++) {
                                        JSONObject wordObj = wordArray.getJSONObject(k);
                                        lineText.append(wordObj.getString("content"));
                                    }
                                    result.recognizedTexts.add(lineText.toString());
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            result.success = false;
            result.code = "-1";
            result.desc = "解析结果失败: " + e.getMessage();
        }
        return result;
    }

    /**
     * 获取所有识别出的文本行
     * @return 文本行列表
     */
    public List<String> getRecognizedTexts() {
        return recognizedTexts;
    }

    /**
     * 获取所有识别出的文本（合并成一行）
     * @return 合并后的文本
     */
    public String getRecognizedText() {
        if (recognizedTexts == null || recognizedTexts.isEmpty()) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (String text : recognizedTexts) {
            sb.append(text).append(" ");
        }
        return sb.toString().trim();
    }

    /**
     * 识别是否成功
     * @return 是否成功
     */
    public boolean isSuccess() {
        return success;
    }

    /**
     * 获取结果码
     * @return 结果码
     */
    public String getCode() {
        return code;
    }

    /**
     * 获取结果描述
     * @return 结果描述
     */
    public String getDesc() {
        return desc;
    }

    /**
     * 获取会话ID
     * @return 会话ID
     */
    public String getSid() {
        return sid;
    }
} 